﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.MembershipProviders;

/// <summary>
/// Selection for all available projects
/// </summary>
public partial class ProjectSelection : ObjectSelection
{
    
    /// <summary>
    /// Event triggered when project selection is changed
    /// </summary>
    public event EventHandler SelectedProjectChangedEvent;

    /// <summary>
    /// Gets the selected project.
    /// </summary>
    /// <value>The selected project.</value>
    public Project SelectedProject
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListProject.SelectedValue) || this.DropDownListProject.SelectedValue=="-1")
            {
                return null;
            }
            else
            {
                return DAOProject.GetProjectById(Int32.Parse(this.DropDownListProject.SelectedValue));
            }
        }         
        set
        {
            this.DropDownListProject.SelectedValue = value.ID.ToString();
        }
    }

    /// <summary>
    /// Gets the selected project ID.
    /// </summary>
    /// <value>The selected project ID.</value>
    public int SelectedProjectID
    {
        get
        {
            if (String.IsNullOrEmpty(this.DropDownListProject.SelectedValue) || this.DropDownListProject.SelectedValue == "-1")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(this.DropDownListProject.SelectedValue);
            }
        }
        set
        {
            SetProjectID(value);
        }
    }

    /// <summary>
    /// Sets the project ID.
    /// </summary>
    /// <param name="projectID">The project ID.</param>
    public void SetProjectID(int projectID)
    {
        ListItem li = DropDownListProject.Items.FindByValue(projectID.ToString());
        if (li == null)
        {
            SetProjectByTeam(-1);
            if (SelectedProjectChangedEvent != null)
            {
                SelectedProjectChangedEvent(this, new EventArgs());
            }
        }
        else
        {
            DropDownListProject.SelectedIndex = projectID;
            if (SelectedProjectChangedEvent != null)
            {
                SelectedProjectChangedEvent(this, new EventArgs());
            }

            li.Selected = true;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="ProjectSelection"/> is enabled.
    /// </summary>
    /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
    public bool Enabled
    {
        get 
        {
            return this.DropDownListProject.Enabled;
        }
        set 
        {
            this.DropDownListProject.Enabled = value;
        }
    } 
    

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadProjects();
        }
    }

    /// <summary>
    /// Loads the projects.
    /// </summary>
    protected void LoadProjects()
    {
        DropDownListProject.DataSource = DAOProject.GetAllProjects();
        DropDownListProject.DataTextField = "Name";
        DropDownListProject.DataValueField = "Id";
        DropDownListProject.DataBind();
        if (this.IsFilter)
        {
            DropDownListProject.Items.Insert(0, TopListItemForFilter("Filters","AnyProject"));
        }
    }

    /// <summary>
    /// Sets the right project in DDL
    /// </summary>
    /// <param name="releaseID">ID of selected release (in DDL for releases)</param>
    public void SetProjectByRelease(int releaseID)
    {

        Project project = DAOProject.GetProjectByReleaseID(releaseID);

        for (int i = 0; i < this.DropDownListProject.Items.Count; i++)
        {
            if (this.DropDownListProject.Items[i].Value == project.ID.ToString())
            {
                this.DropDownListProject.SelectedIndex = i;
                break;
            }
        }
        //ListItem li = DropDownListProject.Items.FindByValue(project.ID.ToString());

        //if (li != null)
        //{
        //    li.Selected = true;
        //    //this.SelectedProjectID = project.ID;
        //}
        //else
        //{
        //    this.DropDownListProject.SelectedValue = "-1";
        //    //this.SelectedProjectID = -1;
        //}
    }

    /// <summary>
    /// Sets the right project in DDL
    /// </summary>
    /// <param name="teamID">ID of selected team (in DDL for teams)</param>
    public void SetProjectByTeam(int teamID)
    {
        if (teamID == -1)
        {
            DropDownListProject.SelectedIndex = -1;
        }
        else
        {
            Project project = DAOProject.GetProjectByTeam(teamID);

            // Select the right project
            ListItem li = DropDownListProject.Items.FindByValue(project.ID.ToString());

            if (li != null)
            {
                li.Selected = true;
                this.SelectedProjectID = project.ID;
            }
            else
            {
                this.DropDownListProject.SelectedValue = "-1";
                this.SelectedProjectID = -1;
            }
        //    for (int i = 0; i < this.DropDownListProject.Items.Count; i++)
        //    {
        //        if (this.DropDownListProject.Items[i].Value == project.ID.ToString())
        //        {
        //            this.DropDownListProject.SelectedIndex = i;
        //            break;
        //        }
        //    }
        }
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        LoadProjects();
    }

    /// <summary>
    /// Selected index changed -> sends event
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void SelectedProjectChanged(object sender, EventArgs e)
    {
        if (this.SelectedProjectChangedEvent != null)
        {
            this.SelectedProjectChangedEvent(sender, e); 
        }         
    }

    /// <summary>
    /// Clears the dropdown lists.
    /// </summary>
    public void Clear(object sender, EventArgs e)
    {
        this.DropDownListProject.SelectedIndex = 0;
        if (this.SelectedProjectChangedEvent != null)
        {
            this.SelectedProjectChangedEvent(sender, e);
        }
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListProject;
    }

    /// <summary>
    /// Loads the label of this web control
    /// </summary>
    /// <returns></returns>
    protected override Label GetLabel()
    {
        return this.LabelProjects;
    }

}
